package org.inspiration.dudu.user.service.impl;

import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.user.mapper.UserLikeMapper;
import org.inspiration.dudu.user.model.dto.UserLikeDTO;
import org.inspiration.dudu.user.model.entity.UserLikeEntity;
import org.inspiration.dudu.user.model.vo.UserLikeVO;
import org.inspiration.dudu.user.service.UserLikeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户点赞服务实现类
 * 提供用户点赞相关的业务逻辑处理
 */
@Service
@RequiredArgsConstructor
public class UserLikeServiceImpl implements UserLikeService {

    private final UserLikeMapper userLikeMapper;

    /**
     * 创建用户点赞记录
     * 
     * @param userLikeDTO 用户点赞数据传输对象
     */
    @Override
    public void createUserLike(UserLikeDTO userLikeDTO) {
        UserLikeEntity userLikeEntity = transToEntity(userLikeDTO);
        userLikeMapper.createUserLike(userLikeEntity);
    }

    /**
     * 删除用户点赞记录
     * 
     * @param userLikeDTO 用户点赞数据传输对象
     */
    @Override
    public void deleteUserLike(UserLikeDTO userLikeDTO) {
        UserLikeEntity userLikeEntity = transToEntity(userLikeDTO);
        userLikeMapper.deleteUserLike(userLikeEntity);
    }

    /**
     * 将用户点赞数据传输对象转换为实体
     * 
     * @param userLikeDTO 用户点赞数据传输对象
     * @return 转换后的用户点赞实体
     */
    @Override
    public UserLikeEntity transToEntity(UserLikeDTO userLikeDTO) {
        UserLikeEntity userLikeEntity = new UserLikeEntity();
        BeanUtils.copyProperties(userLikeDTO, userLikeEntity);
        return userLikeEntity;
    }

    /**
     * 将用户点赞实体转换为视图对象
     * 
     * @param userLikeEntity 用户点赞实体
     * @return 转换后的用户点赞视图对象
     */
    @Override
    public UserLikeVO transToVO(UserLikeEntity userLikeEntity) {
        UserLikeVO userLikeVO = new UserLikeVO();
        BeanUtils.copyProperties(userLikeEntity, userLikeVO);
        return userLikeVO;
    }

    /**
     * 检查用户是否已点赞
     * 
     * @param userId 用户ID
     * @param contentId 内容ID
     * @return 如果已点赞返回true，否则返回false
     */
    @Override
    public Boolean isLiked(Long userId, String contentId) {
        return userLikeMapper.isLiked(userId, contentId) > 0;
    }


    /**
     * 将点赞标记为已读
     */
    @Override
    public void markAsRead(UserLikeDTO userLikeDTO) {
        userLikeMapper.markAsRead(transToEntity(userLikeDTO));
    }

    /**
     * 获取用户最近的点赞记录
     * 
     * @param userId 用户ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 用户最近的点赞记录列表
     */
    @Override
    public List<UserLikeEntity> getLikesByUserIdOrderByDESC(Long userId, Integer pageNum, Integer pageSize) {
        return userLikeMapper.getLikesOrderByDESC(userId, (pageNum - 1) * pageSize, pageSize);
    }

    @Override
    public UserLikeEntity getUserLike(UserLikeDTO userLikeDTO) {
        return userLikeMapper.getUserLike(userLikeDTO);
    }

    @Override
    public List<UserLikeEntity> getUserLikesByTargetUserIdOrderByDESC(Long userId, Integer pageNum, Integer pageSize) {
        return userLikeMapper.getUserLikesByTargetUserIdOrderByDESC(userId, (pageNum - 1) * pageSize, pageSize);
    }


}
